/****************************************************/
/* mcwrapper.cpp                                    */
/****************************************************/

#include "server.h"
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>

using namespace std ;

void makeconfig() {

  ofstream file;
  string input ;
  
  file.open("config") ;
  
  cout << "Creating a new configuration...\n\n" << endl ;

  cout << "Enter the screen name for minecraft server: " ;
  cin >> input ;
  cout << endl ;
  file << input << endl;;
  cout << "Enter the path to minecraft_server.jar (end with a /): " ;
  cin >> input ;
  cout << endl ;
  file << input << endl ;
  cout << "Enter the amount of memory( 1G 512MB etc): " ;
  cin >> input ;
  cout << endl ;
  file << input << endl ;
  file.close() ;
  cout << "Config written. re-run to load it." << endl ;
  

}

int main( int argc, char *argv[] ) {

  ifstream config( "config" ) ;
  int input ;
  string line[SERVER_PARS] ;
  int i ;
  if( config.is_open()) {

    for( i = 0 ; i < SERVER_PARS ; i++ ) getline( config, line[i] ) ;
      
    config.close() ;
  } else { 
    cout << "Failed to open config file" << endl ; 
    makeconfig();  
    return 0 ; 
  }


  server svr( line[0], line[1], line[2] ) ;

  if( argc > 1 ) {
    
    if( argc != 2 ) { cout << "Usage: mcwrapper <start/stop/restart>" << endl ; return 0 ; } 
    
    if( strcmp("start",argv[1]) == 0 ) { svr.start(); return 0 ; }
    if( strcmp("stop",argv[1]) == 0 ) { svr.stop() ; return 0 ; }
    if( strcmp("restart",argv[1]) == 0 ) { svr.restart(); return 0 ; }
     if( strcmp("backup",argv[1]) == 0 ) { svr.backup(); return 0 ; }
    
    cout << "Usage: mcwrapper <start/stop/restart/backup>" << endl ; return 0 ;
    
  }

  do {
    
    cout << "MC Wrapper Commands:" << endl ;
    cout << "[0] quit" << endl ;
    cout << "[1] Start server" << endl ;
    cout << "[2] Stop server" << endl ;
    cout << "[3] Restart server" << endl ;
    cout << "[4] Backup server" << endl ;

    cout << "Enter your choice: ";
    
    cin >> input ;

    switch( input ) {

    case 0:
      return 0 ;
    case 1:
      svr.start() ;
      break ;
    case 2:
      svr.stop() ;
      break ;
    case 3:
      svr.restart() ;
      break ;
    case 4:
      svr.backup() ;
      break ;
    default:
      cout << "Invalid choice." << endl ;

    }
    

  } while( input != 0) ;
  

  return 0 ;


}
